package com.hb.trade.server.center.au.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.center.au.entity.ApiInfo;
import com.hb.core.pojo.center.au.entity.ApiRole;
import com.hb.core.pojo.center.au.mapper.ApiRoleMapper;
import com.hb.core.pojo.enums.ApiFilterType;
import com.hb.core.pojo.parent.MPCommonEntity;
import com.hb.trade.server.center.au.service.ApiInfoService;
import com.hb.trade.server.center.au.service.ApiRoleService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色关联中间表, 用于关联用户角色 服务实现类
 * </p>
 *
 * @author zhaojk
 * @since 2022-03-22
 */
@Service
public class ApiRoleServiceImpl extends ServiceImpl<ApiRoleMapper, ApiRole> implements ApiRoleService {

    protected final ApiInfoService apiInfoService;

    public ApiRoleServiceImpl(ApiInfoService apiInfoService) {
        this.apiInfoService = apiInfoService;
    }

    @Override
    public List<ApiInfo> listApisByRoleIds(List<Long> roleIds, ApiFilterType filterType) {
        if (CollUtil.isEmpty(roleIds)) {
            return null;
        }
        QueryWrapper<ApiRole> wrapper = ApiRole.wrapper("api_id");
        wrapper.in("role_id", roleIds).eq("filter_type", filterType.getCode());
        List<ApiRole> apiRoles = list(wrapper);
        if (CollUtil.isEmpty(apiRoles)) {
            return null;
        }
        List<Long> apiIds = CollUtil.map(apiRoles, ApiRole::getApiId, true);
        return apiInfoService.listByIds(apiIds);
    }
}
